package com.sf.osms.dep.processor.builder;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.sf.osms.dep.biz.ICommonServiceBiz;
import com.sf.osms.dep.domain.Agent;
import com.sf.osms.dep.domain.Router;
import com.sf.osms.dep.processor.EventProcessor;
import com.sf.osms.dep.processor.downloader.EventDownloader;
import com.sf.osms.dep.processor.downloader.FTPEventDownloader;
import com.sf.osms.dep.processor.handler.EventHandler;
import com.sf.osms.dep.processor.handler.TextEventHandler;
import com.sf.osms.dep.processor.parser.CSVEventParser;
import com.sf.osms.dep.processor.parser.EventParser;
import com.sf.osms.dep.processor.parser.XmlEventParser;
import com.sf.osms.dep.processor.parser.excel.ExcelEventParser;
import com.sf.osms.dep.processor.persistor.CommonEventPersistor;
import com.sf.osms.dep.processor.persistor.EventPersistor;

public class EventProcessorFactory {
	
	public static final Map<String,EventDownloader> downloaders;
	public static final Map<String,EventParser> parsers;
	public static final Map<String,EventHandler> handlers;
	public static final Map<String,EventPersistor> persistors;
	
	static{
		downloaders = new HashMap<String, EventDownloader>();
		downloaders.put("FTP", new FTPEventDownloader());
		parsers = new HashMap<String, EventParser>();
		parsers.put("TXT", new CSVEventParser());
		parsers.put("EXCEL", new ExcelEventParser());
		parsers.put("XML", new XmlEventParser());
		
		handlers = new HashMap<String, EventHandler>();
		handlers.put("TXT", new TextEventHandler());
		
		persistors = new HashMap<String, EventPersistor>();
		persistors.put("COMMON", new CommonEventPersistor());
	}
	
	public static EventProcessor getEventProcessor(Agent agent,ICommonServiceBiz commonService){
		String fileSuffix = agent.getAgentFileMap().getFileSuffix();
		if(StringUtils.isEmpty(fileSuffix)){
			fileSuffix = "TXT";
		}
		fileSuffix = fileSuffix.toUpperCase();
		if("CSV".equals(fileSuffix)){
			fileSuffix = "TXT";
		}else if("XLS".equals(fileSuffix) || "XLSX".equals(fileSuffix)){
			fileSuffix = "EXCEL";
		}else if("XML".equals(fileSuffix)){
			fileSuffix = "XML";
		}else{
			fileSuffix = "TXT";
		}
		
		
		EventDownloader downloader = downloaders.get("FTP");
		EventParser csvParser = parsers.get(fileSuffix);
		EventHandler handler = handlers.get("TXT");
		CommonEventPersistor persistor = (CommonEventPersistor) persistors.get("COMMON");
		persistor.setCommonService(commonService);
		
		EventProcessor<String[], Router> eventProcessor = new EventProcessor<String[], Router>(downloader, csvParser, handler, persistor,commonService);
		return eventProcessor;
	}
	
	public static FileProcessor getFileProcessor(Agent agent){
		String fileSuffix = agent.getAgentFileMap().getFileSuffix();
		if(StringUtils.isEmpty(fileSuffix)){
			fileSuffix = "TXT";
		}
		fileSuffix = fileSuffix.toUpperCase();
		if("CSV".equals(fileSuffix)){
			fileSuffix = "TXT";
		}else if("XLS".equals(fileSuffix) || "XLSX".equals(fileSuffix)){
			fileSuffix = "EXCEL";
		}else if("XML".equals(fileSuffix)){
			fileSuffix = "XML";
		}else{
			fileSuffix = "TXT";
		}
		
		EventParser parser = parsers.get(fileSuffix);
		EventHandler handler = handlers.get("TXT");
		
		FileProcessor<String[], Router> fileProcessor = new FileProcessor<String[], Router>(parser,handler);
		return fileProcessor;
		
	}
}
